package com.mhc.suzuki.service.impl;

import com.mhc.suzuki.bo.AnnexListBO;
import com.mhc.suzuki.dal.model.AnnexListDO;
import com.mhc.suzuki.dal.query.AnnexListQuery;
import com.mhc.suzuki.manager.AnnexListManager;
import com.mhc.suzuki.service.AnnexListService;
import com.mhc.suzuki.util.BeanCopierUtil;
import com.mhc.suzuki.vo.AnnexListVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class AnnexListServiceImpl implements AnnexListService {

    @Autowired
    private AnnexListManager annexListManager;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AnnexListVO addAnnex(AnnexListDO annexListDO) {
        annexListManager.insertSelective(annexListDO);
        AnnexListVO annexListVO = new AnnexListVO();
        annexListVO.setAnnexId(annexListDO.getAnnexId());
        return annexListVO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAnnex(Long annexId) {
        AnnexListDO annexListDO = new AnnexListDO();
        annexListDO.setAnnexId(annexId);
        annexListManager.deleteByPrimaryKey(annexListDO);
    }

    @Override
    public List<AnnexListVO> getAnnexList(String annexBatchNo, Integer annexType) {
        AnnexListQuery query = new AnnexListQuery();
        query.createCriteria().andAnnexBatchNoEqualTo(annexBatchNo).andAnnexTypeEqualTo(annexType);
        List<AnnexListDO> annexListDOList = annexListManager.selectByQuery(query);
        List<AnnexListVO> annexListVOS = new ArrayList<>();
        annexListDOList.forEach(annexListDO -> {
            AnnexListVO annexListVO = BeanCopierUtil.convert(annexListDO, AnnexListVO.class);
            annexListVOS.add(annexListVO);
        });

        return annexListVOS;
    }

    /**
     * 根据附件批次号、附件类型、附件分组查询附件信息
     *
     * @param annexBatchNo 附件批次号
     * @return
     */
    @Override
    public List<AnnexListBO> getAnnexList(String annexBatchNo) {
        Map<String, Object> paramMap = new HashMap<>(64);
        paramMap.put("annexBatchNo", annexBatchNo);    //附件批次号
        return annexListManager.selectByAnnexList(paramMap);
    }

    @Override
    public AnnexListDO getAnnex(Long annexId) {
        return annexListManager.selectByPrimaryKey(annexId);
    }
}
